package priv.yiyuan.example.composite.approvar1.handler;

import priv.yiyuan.example.composite.approvar1.ApprovalRequest;
import priv.yiyuan.example.composite.approvar1.Approver;

/**
 * 经理级处理者
 * <p>
 * 责任链模式-具体处理者
 *
 * @author YiYuan
 * @data 2025/8/29
 * @apoNote
 */
public class ManagerHandler implements ApprovalHandler {

    private ApprovalHandler nextHandler;

    private Approver managerApprover;

    public ManagerHandler(Approver managerApprover) {
        this.managerApprover = managerApprover;
    }

    @Override
    public void setNextHandler(ApprovalHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    @Override
    public void handleRequest(ApprovalRequest request) {

        System.out.println("\n责任链中的经理处理者 (" + managerApprover.getClass().getSimpleName() + ") 尝试处理请求...");
        if (request.getAmount() <= managerApprover.getApprovalLimit()) {
            managerApprover.processRequest(request);
            System.out.println("  请求被经理处理者成功处理。");
        }
        else if (nextHandler != null) {
            System.out.println("  经理处理者无法处理 (超出限额: " + managerApprover.getApprovalLimit() + "), 传递给下一个处理者。");
            nextHandler.handleRequest(request);
        }
        else {
            System.out.println("  请求未被处理, 且无后续处理者。");
        }
    }
}
